//引入express模块
const express=require("express");
//引入连接池模块
const pool=require("../pool.js")
//创建订单详情路由器对象
const r=express.Router();
//订单详情添加
r.post("/detail_reg",(req,res)=>{
	var $did=req.body.did
	var $order_id=req.body.order_id
	var $product_id=req.body.product_id
	var $count=req.body.count
		//console.log($count);
	var sql="insert into vip_order_detail(did,order_id,product_id,count) values(?,?,?,?)"
	pool.query(sql,[$did,$order_id,$product_id,$count],(err,result)=>{
		if(err) throw err;
		//console.log(err)
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})

r.get("/detail_reg/check/:did",(req,res)=>{
	var $did=req.params.did;
	var sql="select * from vip_order_detail where did=?"
	pool.query(sql,[$did],(err,result)=>{
		if(err){
			throw err
		}
		if(result.length>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
	
})

//订单详情搜索
r.get("/detail_search/:did",(req,res)=>{
	var $did=req.params.did;
	var sql="select * from vip_order_detail where did=?"
	pool.query(sql,[$did],(err,result)=>{
		if(err){
			throw err
		}
		if(result.length>0){
			res.send(result)
		}else{
			res.send("0")
		}
	})
	
})

//订单详情修改
r.put("/detail_revise",(req,res)=>{
	var obj=req.body;
	var sql="update vip_order_detail set? where did=?"
	pool.query(sql,[obj,obj.did],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})

//订单详情删除
r.delete("/detail_del/:did",(req,res)=>{
	var $did=req.params.did;
	var sql="delete from vip_order_detail where did=?"
	pool.query(sql,[$did],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})

//导出订单详情路由器对象
module.exports=r;